package com.caroil.caroilservice.activitys.login.fragment;

import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.caroil.caroilservice.R;
import com.caroil.caroilservice.activitys.login.vmode.LoginMode;
import com.caroil.caroilservice.base.BaseApplation;
import com.caroil.caroilservice.base.BaseFragment;
import com.caroil.caroilservice.base.ColorFile;
import com.caroil.caroilservice.util.NetWorkUtil;
import com.caroil.caroilservice.util.TimeCountUtil;
import com.caroil.caroilservice.util.ViewBgUtil;
import com.caroil.caroilservice.view.TitleControlView;
import com.caroil.caroilservice.view.dialog.LoadingDialog;
import com.king.base.util.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 手机验证码登录
 */
public class PhoneCodeLoginFragment extends BaseFragment {

    public static PhoneCodeLoginFragment newInstance() {
        PhoneCodeLoginFragment phoneCodeLoginFragment = new PhoneCodeLoginFragment();
        return phoneCodeLoginFragment;
    }

    @BindView(R.id.tv_code)
    TextView tvCode;
    @BindView(R.id.edit_phone)
    TextView editPhone;
    @BindView(R.id.edit_code)
    TextView editCode;
    @BindView(R.id.progressbar)
    LoadingDialog mProgressbar;
    @BindView(R.id.btn_login)
    Button btn_login;
    @BindView(R.id.checkbox)
    ImageView checkBox;
    private TimeCountUtil timeCountUtil;
    private LoginMode loginMode;
    private boolean isSelect = true;

    @Override
    protected int initLayout() {
        return R.layout.ac_phonecode;
    }

    @Override
    protected void initView(View view) {
        setBarState(false);
        initTitle(view);
        tvCode.setBackground(ViewBgUtil.setBg(12, Color.parseColor(ColorFile.BULE)));
        btnLoginState(false);
        addEditState();
    }

    @Override
    protected void initData(Context mContext) {
        loginMode = getViewModelProvider().get(LoginMode.class);
    }

    @Override
    protected void loadMethod() {

    }

    @OnClick({R.id.btn_login, R.id.tv_code, R.id.ly_checkbox, R.id.checkbox, R.id.tv_ysxy})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_login://登录
                mProgressbar.show();
                break;
            case R.id.tv_code://获取短信验证码
                if (editPhone.getText().toString().length() != 11) {
                    ToastUtils.showToast(BaseApplation.instance(), getResources().getString(R.string.RightPhone));
                    return;
                }
                if (NetWorkUtil.check(getContext())) {
                    timeCountUtil = new TimeCountUtil(getActivity(), 60000, 1000, tvCode);
                    timeCountUtil.start();
                    loginMode.getPhoneCode(context, editPhone.getText().toString());
                }
                break;
            case R.id.ly_checkbox://隐私协议选中开关
                xySelect();
                break;
            case R.id.checkbox://隐私协议选中开关
                xySelect();
                break;
            case R.id.tv_ysxy://隐私协议
                start(AgreementWebFragment.newInstance());
                break;
        }
    }

    /**
     * 输入框的状态监听
     */
    private void addEditState() {
        editPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!isSelect) {
                    return;
                }
                if (s.length() == 11 && editCode.getText().toString().length() > 0) {
                    btnLoginState(true);
                } else {
                    btnLoginState(false);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        editCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!isSelect) {
                    return;
                }
                if (s.length() > 0 && editPhone.getText().toString().length() == 11) {
                    btnLoginState(true);
                } else {
                    btnLoginState(false);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    /**
     * 登录按钮的状态判定
     *
     * @param b
     */
    private void btnLoginState(boolean b) {
        if (b) {
            btn_login.setBackgroundResource(R.mipmap.login_btn);
            btn_login.setTextColor(Color.parseColor("#ffffff"));
            btn_login.setClickable(true);
        } else {
            btn_login.setBackgroundResource(R.mipmap.login_btn_no);
            btn_login.setTextColor(Color.parseColor("#999999"));
            btn_login.setClickable(false);
        }

    }

    private void xySelect() {
        if (isSelect) {
            isSelect = false;
            checkBox.setImageResource(R.mipmap.icon_login_check);
            btn_login.setBackgroundResource(R.mipmap.login_btn_no);
            btn_login.setTextColor(Color.parseColor("#999999"));
            btn_login.setClickable(false);
        } else {
            isSelect = true;
            checkBox.setImageResource(R.mipmap.icon_login_checkpre);
            if (editPhone.length() == 11 && editCode.getText().toString().length() > 0) {
                btnLoginState(true);
            } else {
                btnLoginState(false);
            }
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (timeCountUtil != null)
            timeCountUtil.cancel();
    }

    /**
     * 设置标题栏
     */
    private void initTitle(View view) {
        TitleControlView t = new TitleControlView(view, () -> goBack(getActivity()));
        t.setTitle("");
    }
}
